package pl.prv.polanie.client.widgets.schedule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSSubjectDTO;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSSubjectTypeDTO;
import pl.prv.polanie.client.model.Cache;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Janusz Prokulewicz
 * Created: Nov 30, 2008
 */
public class PrintoutScheduleReport extends Composite {
	
	private VerticalPanel mainFT = new VerticalPanel();
	
	public PrintoutScheduleReport() {
		initWidget(mainFT);
	}
	
	
	
	/**
	 * Uaktualnia wyswietlane wartosci w raporcie
	 * (raport jest widoczny pod rozkladem po wydruku).
	 */
	public void showDeclarationsSummary(Schedule schedule) {
		
		long groupID = schedule.getFilterPropertyGroupID();
		
		//pobrac do kolekcji wszustkie declarations z podzialu
		CSDeclarationDTO[] ds = null;
		
		if (groupID != -1) {
			ds = Cache.getDeclarationsByGroupID(groupID);
		}
		

//		List<CSSubjectEventDTO> list = schedule.getAllScheduledSubjectEvents();
		
		if (ds == null) {
			return;
		}
		
		
		
//		Planner.log("PrintoutScheduleReport.refresh: " + "REFRESH ilosc SE " + list.size());
		//mapa <subjectNameID, list<subjectTypeID, iloscGodzin>>
		Map<Long, List<SubjectTypeAndDuration>> map = new HashMap<Long, List<SubjectTypeAndDuration>>();

		for (int i = 0; i < ds.length; i++) {
			CSDeclarationDTO csSE = ds[i];
			
			//pobrac subjectNameID
			CSSubjectDTO csS = Cache.getCSSubjectDTOByID(csSE.getSubjectID());
			Long snID = new Long(csS.getSubjectNameID());
			long stID = csS.getSubjectTypeID();
			
			List<SubjectTypeAndDuration> stadList = map.get(snID);
			if (stadList == null) {
				stadList = new ArrayList<SubjectTypeAndDuration>();
				map.put(snID, stadList);
			}
			
			//znalezc w liscie element z takim samym subjectTypeID
			SubjectTypeAndDuration stad = null;
			for (int j = 0; j < stadList.size(); j++) {
				SubjectTypeAndDuration stnd = stadList.get(j);
				if (stnd.getSubjectTypeID() == stID) {
					stad = stnd;
				}
			}
			
			if (stad == null) {
				stad = new SubjectTypeAndDuration();
				stad.setSubjectTypeID(stID);
				stadList.add(stad);
			}
			
			
			stad.setDuration(stad.getDuration() + csSE.getDuration().intValue());
		}
		
		
		mainFT.clear();
		Set<Long> keys = map.keySet();
		
		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
			Long key = (Long) iterator.next();
			List<SubjectTypeAndDuration> lista = map.get(key);
			
			CSSubjectNameDTO csSN = Cache.getCSSubjectNameDTOByID(key.longValue());
			
			String subjectS = csSN.getSymbol() + " - " + csSN.getName() + " ";
			
			for (int i = 0; i < lista.size(); i++) {
				SubjectTypeAndDuration stad = lista.get(i);
				
				CSSubjectTypeDTO st = Cache.getCSSubjectTypeDTOByID(stad.getSubjectTypeID());
				
				String s = (stad.getDuration()/0.75/60) + st.getInitial();
				if (i < lista.size()-1) {
					s = s + ", ";
				}
				
				subjectS = subjectS + s; 
			}
			
			mainFT.add(new Label(subjectS));
		}
		
	}
	
	
	
	
	
	
	
	
	
	/**
	 * Uaktualnia wyswietlane wartosci w raporcie.
	 */
	public void showScheduledSESummary(Schedule schedule) {
		Planner.log("PrintoutScheduleReport.refresh: " + "REFRESH ");
		
		
		//pobrac do listy wszustkie csSE z podzialu
		List<CSSubjectEventDTO> list = schedule.getAllScheduledSubjectEvents();
		
		Planner.log("PrintoutScheduleReport.refresh: " + "REFRESH ilosc SE " + list.size());
		//mapa <subjectNameID, list<subjectTypeID, iloscGodzin>>
		Map<Long, List<SubjectTypeAndDuration>> map = new HashMap<Long, List<SubjectTypeAndDuration>>();

		for (int i = 0; i < list.size(); i++) {
			CSSubjectEventDTO csSE = list.get(i);
			
			//pobrac subjectNameID
			CSSubjectDTO csS = Cache.getCSSubjectDTOByID(csSE.getSubjectID());
			Long snID = new Long(csS.getSubjectNameID());
			long stID = csS.getSubjectTypeID();
			
			List<SubjectTypeAndDuration> stadList = map.get(snID);
			if (stadList == null) {
				stadList = new ArrayList<SubjectTypeAndDuration>();
				map.put(snID, stadList);
			}
			
			//znalezc w liscie element z takim samym subjectTypeID
			SubjectTypeAndDuration stad = null;
			for (int j = 0; j < stadList.size(); j++) {
				SubjectTypeAndDuration stnd = stadList.get(j);
				if (stnd.getSubjectTypeID() == stID) {
					stad = stnd;
				}
			}
			
			if (stad == null) {
				stad = new SubjectTypeAndDuration();
				stad.setSubjectTypeID(stID);
				stadList.add(stad);
			}
			
			
			stad.setDuration(stad.getDuration() + csSE.getDuration().intValue());
		}
		
		
		mainFT.clear();
		Set<Long> keys = map.keySet();
		
		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
			Long key = (Long) iterator.next();
			List<SubjectTypeAndDuration> lista = map.get(key);
			
			CSSubjectNameDTO csSN = Cache.getCSSubjectNameDTOByID(key.longValue());
			
			String subjectS = csSN.getSymbol() + " - " + csSN.getName() + " ";
			
			for (int i = 0; i < lista.size(); i++) {
				SubjectTypeAndDuration stad = lista.get(i);
				
				CSSubjectTypeDTO st = Cache.getCSSubjectTypeDTOByID(stad.getSubjectTypeID());
				
				String s = stad.getDuration() + st.getInitial();
				if (i < lista.size()-1) {
					s = s + ", ";
				}
				
				subjectS = subjectS + s; 
			}
			
			mainFT.add(new Label(subjectS));
		}
		
	}
	
	
	
	
	private class SubjectTypeAndDuration {
		private long subjectTypeID;
		private int duration;
		
		public long getSubjectTypeID() {
			return subjectTypeID;
		}
		public void setSubjectTypeID(long subjectTypeID) {
			this.subjectTypeID = subjectTypeID;
		}
		public int getDuration() {
			return duration;
		}
		public void setDuration(int duration) {
			this.duration = duration;
		}
	}
	
}
